/*
 * BTInquiryListener.java
 *
 * Created on 2 September 2007, 12:51
 *
 */

package digitrix.network.bt;

import java.util.Enumeration;
import java.util.Vector;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

/**
 *
 * @author Josh
 */
public class BTInquiryListener implements DiscoveryListener {
    
    private BTManager _btManager = null; 
    private Vector _foundDevices = null;
    
    /** Creates a new instance of BTInquiryListener */
    public BTInquiryListener(  BTManager btManager ) {
        _btManager = btManager; 
        _foundDevices = new Vector();         
    }
    
    public void deviceDiscovered( RemoteDevice btDevice, DeviceClass cod){
            int major = cod.getMajorDeviceClass();
            if( major == 0x0200 ){                
               // its another phone so store it in our list if we haven't already
                if( !_foundDevices.contains( btDevice ) && !_btManager.deviceAlreadyExamined( btDevice.getBluetoothAddress() ) ){
                    _foundDevices.addElement( btDevice ); 
                    
                    // check to see if we have found the max number of devices to process 
                    // (the reason this is done is because device and service search is time consuming
                    // so we want to limit the number of devices we process) 
                    if( _foundDevices.size() >= BTManager.MAX_DEVICES )
                        _btManager.getDiscoveryAgent().cancelInquiry( this ); 
                }
            }
        }
        
        public void inquiryCompleted(int dicType ){
            synchronized( this ){
                this.notifyAll();
            }
            
        }
        
        // service listeners declared not not handled in this class
        public void servicesDiscovered( int transID, ServiceRecord[] servRecord){}
        
        public void serviceSearchCompleted( int transID, int respCode ){}
        
        public Enumeration getFoundDevices(){
            return _foundDevices.elements(); 
        }    
}
